package com.ruoyi.project.channel.bannerInfoConfig.controller;

import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.channel.financialProductConfig.domain.FinancialProductConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.bannerInfoConfig.domain.BannerInfoConfig;
import com.ruoyi.project.channel.bannerInfoConfig.service.IBannerInfoConfigService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 轮播图配置信息操作处理
 * 
 * @author CAIN
 * @date 2021-01-09
 */
@Controller
@RequestMapping("/channel/bannerInfoConfig")
public class BannerInfoConfigController extends BaseController
{
    private String prefix = "channel/bannerInfoConfig";
	
	@Autowired
	private IBannerInfoConfigService bannerInfoConfigService;
	
	@RequiresPermissions("channel:bannerInfoConfig:view")
	@GetMapping()
	public String bannerInfoConfig()
	{
	    return prefix + "/bannerInfoConfig";
	}
	
	/**
	 * 查询轮播图配置列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(BannerInfoConfig bannerInfoConfig)
	{
		startPage();
        List<BannerInfoConfig> list = bannerInfoConfigService.selectBannerInfoConfigList(bannerInfoConfig);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出轮播图配置列表
	 */
	@RequiresPermissions("channel:bannerInfoConfig:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BannerInfoConfig bannerInfoConfig)
    {
    	List<BannerInfoConfig> list = bannerInfoConfigService.selectBannerInfoConfigList(bannerInfoConfig);
        ExcelUtil<BannerInfoConfig> util = new ExcelUtil<BannerInfoConfig>(BannerInfoConfig.class);
        return util.exportExcel(list, "bannerInfoConfig");
    }
	
	/**
	 * 新增轮播图配置
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存轮播图配置
	 */
	@RequiresPermissions("channel:bannerInfoConfig:add")
	@Log(title = "轮播图配置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(BannerInfoConfig bannerInfoConfig)
	{		
		return toAjax(bannerInfoConfigService.insertBannerInfoConfig(bannerInfoConfig));
	}

	/**
	 * 修改轮播图配置
	 */
	@GetMapping("/edit/{bannerId}")
	public String edit(@PathVariable("bannerId") Integer bannerId, ModelMap mmap)
	{
		BannerInfoConfig bannerInfoConfig = bannerInfoConfigService.selectBannerInfoConfigById(bannerId);
		mmap.put("bannerInfoConfig", bannerInfoConfig);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存轮播图配置
	 */
	@RequiresPermissions("channel:bannerInfoConfig:edit")
	@Log(title = "轮播图配置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(BannerInfoConfig bannerInfoConfig)
	{		
		return toAjax(bannerInfoConfigService.updateBannerInfoConfig(bannerInfoConfig));
	}
	
	/**
	 * 删除轮播图配置
	 */
	@RequiresPermissions("channel:bannerInfoConfig:remove")
	@Log(title = "轮播图配置", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(bannerInfoConfigService.deleteBannerInfoConfigByIds(ids));
	}
	/**
	 * 保存头像
	 */
	@Log(title = "上传轮播图片", businessType = BusinessType.UPDATE)
	@PostMapping("/addBannelUrl")
	@RequiresPermissions("channel:bannerInfoConfig:edit")
	@ResponseBody
	public AjaxResult addAvatar(@RequestParam("avatarfile") MultipartFile file) {
		try {
			if (!file.isEmpty()) {
				String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
				BannerInfoConfig config = new BannerInfoConfig();
				config.setUseStatus("0");
				config.setBannerUrl(avatar);
				bannerInfoConfigService.insertBannerInfoConfig(config);
				return AjaxResult.success("上传成功",avatar);
			}
			return error();
		} catch (Exception e) {
			return error(e.getMessage());
		}
	}

	/**
	 * 保存头像
	 */
	@Log(title = "更新轮播图片", businessType = BusinessType.UPDATE)
	@PostMapping("/updateBannelUrl/{bannerId}")
	@RequiresPermissions("channel:bannerInfoConfig:edit")
	@ResponseBody
	public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file,@PathVariable("bannerId") Integer bannerId) {
		try {
			if (!file.isEmpty()) {
				BannerInfoConfig bannerInfoConfig = bannerInfoConfigService.selectBannerInfoConfigById(bannerId);
				String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
				bannerInfoConfig.setBannerUrl(avatar);
				return toAjax(bannerInfoConfigService.updateBannerInfoConfig(bannerInfoConfig));
			}
			return error();
		} catch (Exception e) {
			return error(e.getMessage());
		}
	}

	/**
	 * 用户状态修改
	 */
	@Log(title = "用户管理", businessType = BusinessType.UPDATE)
	@PostMapping("/changeStatus")
	@RequiresPermissions("channel:bannerInfoConfig:edit")
	@ResponseBody
	public AjaxResult changeStatus(BannerInfoConfig userLevelConfig) {
		return toAjax(bannerInfoConfigService.updateBannerInfoConfig(userLevelConfig));
	}
}
